#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int dx[8] = { 0, 0, 1, 1, 1, -1, -1, -1 };
    int dy[8] = { 1, -1, 0, 1, -1, 0, 1, -1 };
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        if (board[click[0]][click[1]] == 'M')
        {
            board[click[0]][click[1]] = 'X';
            return board;
        }
        dfs(board, click[0], click[1]);
        return board;
    }
    void dfs(vector<vector<char>>& board, int x, int y)
    {
        int count = 0;
        for (int k = 0; k < 8; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < board.size() && b >= 0 && b < board[a].size() && board[a][b] == 'M') count++;
        }
        if (count)
        {
            board[x][y] = count + '0';
            return;
        }
        for (int k = 0; k < 8; k++)
        {
            board[x][y] = 'B';
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < board.size() && b >= 0 && b < board[a].size() && board[a][b] == 'E') dfs(board, a, b);
        }
    }
};